<template>
  <div id="myCollect">
    <div class="haerder">
      <div class="haerder-box">
        <div class="left" @click="goback()">
          <img src="https://mall.leyifan.com/static/h5/new_images/goods_back.png" alt="" />
        </div>
        <div class="name">我的收藏</div>
      </div>
    </div>
    <van-tabs v-model:active="activeTab"  @click-tab="onClickTab">
      <van-tab title="商品收藏">
        <div class="content" v-if="Object.keys(collections).length > 0">
          <div class="box">
            <div class="nav">
              <div class="left">
                当前共
                <span class="num">{{ Object.keys(collections).length }}</span>
                件商品
              </div>
              <div class="right" @click="shouc()">{{ isShow ? '取消' : '管理' }}</div>
            </div>
            <!-- 商品收藏内容 -->
            <div class="centent">
              <van-checkbox-group v-model="checked" ref="checkboxGroup">
                <div
                  class="list_item"
                  v-for="item in collections"
                  :key="item.id"
                  style="margin-bottom: 30rem"
                >
                  <div class="centent_inp" v-if="isShow">
                    <van-checkbox :name="item.id"></van-checkbox>
                  </div>
                  <div class="pictrue">
                    <img
                      :src="
                        item.item.productInfo.image.replace(
                          'https://static.mercdn.net',
                          'https://image1.leyifan.cn/mercari-cdn'
                        )
                      "
                      alt=""
                    />
                  </div>
                  <div class="list_right">
                    <li class="list1">{{ item.item.productInfo.storeName }}</li>
                    <li class="list2">订阅通知</li>
                    <li class="list3">
                      <span style="font-size: 13rem">{{ item.item.productInfo.price }}</span
                      >日元
                    </li>
                  </div>
                </div>
              </van-checkbox-group>
            </div>
          </div>
        </div>
        <div class="out" v-else>
          <div class="bg">
            <img src="https://mall.leyifan.com/pages/users/static/noCollectionTxt.png" alt="" />
          </div>
        </div>
      </van-tab>
      <van-tab title="店铺收藏">
        <div class="content" v-if="Object.keys(storeCollections).length > 0">
          <!-- 店铺收藏内容 -->
          <div class="box">
            <div class="nav">
              <div class="left">
                当前共
                <span class="num">{{ Object.keys(storeCollections).length }}</span>
                家店铺
              </div>
              <div class="right" @click="shouc()">{{ isShow ? '取消' : '管理' }}</div>
            </div>
            <div class="centent">
              <van-checkbox-group v-model="storeChecked" ref="storeCheckboxGroup">
                <div
                  class="list_item"
                  v-for="item in storeCollections"
                  :key="item.id"
                  style="margin-bottom: 30rem"
                >
                  <div class="centent_inp" v-if="isShow">
                    <van-checkbox :name="item.id"></van-checkbox>
                  </div>
                  <div class="pictrue">
                    <img
                      :src="
                        item.item.photoUrl.replace(
                          'https://static.mercdn.net',
                          'https://image1.leyifan.cn/mercari-cdn'
                        )
                      "
                      alt=""
                    />
                  </div>
                  <div class="list_right">
                    <li class="list1">{{ item.item.introduction }}</li>
                    <li class="list2">已认证</li>
                    <li class="list3">订阅通知</li>
                  </div>
                </div>
              </van-checkbox-group>
            </div>
          </div>
        </div>
      </van-tab>
    </van-tabs>
    <div class="buttom_tab" v-if="isShow">
      <div class="tab_left">
        <div class="inp">
          <!-- <input type="checkbox" name="" id="" /> -->
        </div>
        <div class="checkAll">
          <span @click="checkallfun" v-if="!checkAllFlag">全选</span>
          <span @click="toggleallfun" v-else>取消全选</span>
        </div>
      </div>
      <div class="tab_right">
        <div class="btn">
          <span @click="removefun">取消收藏</span>
        </div>
      </div>
    </div>
  </div>
</template>




<script setup lang="ts">
import { ref, watch } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()
const isShow = ref(false)
const activeTab = ref(0)

const collections = ref(JSON.parse(localStorage.getItem('collections') || '{}'))
const collectedItemIds = ref(Object.keys(collections.value))

const storeCollections = ref(JSON.parse(localStorage.getItem('collectionspeople') || '{}'))
const collectedStoreIds = ref(Object.keys(storeCollections.value))
console.log(collectedItemIds.value, collections.value)
console.log(collectedStoreIds.value, storeCollections.value)

let checkAllFlag = ref(false)
let storeCheckAllFlag = ref(false)

// 商品收藏多选框
let checked = ref([])
let checkboxGroup = ref(null)

// 店铺收藏多选框
let storeChecked = ref([])
let storeCheckboxGroup = ref(null)

const checkAll = () => {
  if (checkboxGroup.value) {
    checkboxGroup.value.toggleAll(true)
  }
  checkAllFlag.value = true
  
}
const onClickTab = function(){
  // checkboxGroup.value.toggleAll()
  isShow.value = false
  checkAllFlag.value = false
}
const toggleAll = () => {
  if (checkboxGroup.value) {
    checkboxGroup.value.toggleAll(false)
  }
  checkAllFlag.value = false
}

const storeCheckAll = () => {
  if (storeCheckboxGroup.value) {
    storeCheckboxGroup.value.toggleAll(true)
  }
  checkAllFlag.value = true
}

const storeToggleAll = () => {
  if (storeCheckboxGroup.value) {
    storeCheckboxGroup.value.toggleAll(false)
  }
  checkAllFlag.value = false
}

const checkallfun = function () {
  if (activeTab.value == 0) {
    checkAll()
  } else {
    storeCheckAll()
  }
}

const toggleallfun = function () {
  if (activeTab.value == 0) {
    toggleAll()
  } else {
    storeToggleAll()
  }
}

const goback = () => {
  router.go(-1)
}

const shouc = () => {
  isShow.value = !isShow.value
}

const removeSelectedItems = () => {
  checked.value.forEach(id => {
    delete collections.value[id]
  })
  checked.value = []
  collectedItemIds.value = Object.keys(collections.value)
  localStorage.setItem('collections', JSON.stringify(collections.value))
  checkAllFlag.value = false
}

const removeSelectedStores = () => {
  storeChecked.value.forEach(id => {
    delete storeCollections.value[id]
  })
  storeChecked.value = []
  collectedStoreIds.value = Object.keys(storeCollections.value)
  localStorage.setItem('storeCollections', JSON.stringify(storeCollections.value))
  storeCheckAllFlag.value = false
}
const removefun= () => {
  if(activeTab.value==0){
    removeSelectedItems()
  }else{
    removeSelectedStores()
  }
}
watch(
  () => collections.value,
  (newVal) => {
    collectedItemIds.value = Object.keys(newVal)
    localStorage.setItem('collections', JSON.stringify(newVal))
  },
  { deep: true }
)

watch(
  () => storeCollections.value,
  (newVal) => {
    collectedStoreIds.value = Object.keys(newVal)
    localStorage.setItem('storeCollections', JSON.stringify(newVal))
  },
  { deep: true }
)
</script>

<style lang="less" scoped>
#myCollect {
  height: 100vh;
  overflow: hidden;
  background-color: #f5f5f5;
  .haerder {
    background-color: rgb(255, 255, 255);
    position: fixed;
    z-index: 5;
    width: 100%;
    top: 0;
    .haerder-box {
      border-bottom: 1rem solid rgba(92, 90, 90, 0.1);
      padding-top: 0rem;
      height: 42rem;
      min-height: 42rem;
      padding-right: 15rem;
      display: flex;
      align-items: center;
      background-color: #fff;
      width: 375rem;
      position: fixed;
      z-index: 4;
      top: 0;
      .left {
        margin-left: 15rem;
        img {
          width: 16rem;
          height: 16rem;
        }
      }
      .name {
        margin-left: 15rem;
        text-align: center;
        font-weight: 700;
        white-space: nowrap;
        text-overflow: ellipsis;
      }
    }
  }
  .van-tabs {
    margin-top: 42rem; /* 确保 Tab 选项卡在头部下方 */
  }
  .content {
    padding: 15rem;
    .box {
      padding-top: 15rem;
      .nav {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
        width: 345rem;
        max-width: 345rem;
        line-height: 45rem;
        height: 45rem;
        background-color: #fff;
        padding: 0 12rem;
        -webkit-box-sizing: border-box;
        box-sizing: border-box;
        font-size: 14rem;
        color: #282828;
        border-bottom: 1rem solid #eee;
        border-top-left-radius: 7rem;
        border-top-right-radius: 7rem;
        margin-top: 15rem;
        margin-right: 15rem;
        .left {
          .num {
            color: #ff3d58;
          }
        }
      }
      .centent {
        background-color: #fff;
        padding-left: 12rem;
        min-height: 90rem;
        padding: 14rem;
        margin-bottom: 10rem;
        border-radius: 5rem;
        .list_item {
          width: 320rem;
          height: 67rem;
          display: flex;
          flex-wrap: nowrap;
          .centent_inp {
            line-height: 67rem;
          }
          .pictrue {
            width: 60rem;
            height: 60rem;
            min-width: 75rem;
            margin-right: 10rem;
            position: relative;
            overflow: hidden;
            text-align: center;
            img {
              width: 60rem;
              height: 70rem;
            }
          }
          .list_right {
            flex: 1;
            li {
              font-size: 14rem;
              list-style: none;
              width: 235rem;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: nowrap;
            }
            .list1 {
              margin-bottom: 5rem;
            }
            .list2 {
              color: #b22222;
            }
            .list3 {
              color: #ff3d58;
            }
          }
        }
        .list_bottom {
          width: 100%;
          height: 40rem;
        }
      }
    }
  }
  .out {
    width: 100%;
    height: 100%;
    .bg {
      position: relative;
      text-align: center;
      span {
        position: absolute;
        z-index: 10000;
        bottom: 200rem;
        display: block;
      }
    }
  }
  .buttom_tab {
    z-index: 9;
    width: 100%;
    height: 48rem;
    background-color: #fff;
    position: fixed;
    padding: 0 15rem;
    box-sizing: border-box;
    border-top: 0.5rem solid #eee;
    border-bottom: 1rem solid #eee;
    bottom: 0rem;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    line-height: 48rem;
    .tab_left {
      display: flex;
      .inp {
        padding-top: 2rem;
      }
      .checkAll {
        font-size: 14rem;
        color: #282828;
        margin-left: 8rem;
      }
    }
    .tab_right {
      line-height: 48rem;
      .btn {
        margin-top: 8rem;
        span {
          display: block;
          font-size: 14rem;
          color: #999;
          border-radius: 15rem;
          border: 1rem solid #999;
          width: 80rem;
          height: 30rem;
          text-align: center;
          line-height: 30rem;
        }
      }
    }
  }
}
</style>